package com.dinstar.dinlink.broadcast;

import android.app.Activity;
import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.dinstar.dinlink.activity.MessageActivity;
import com.dinstar.dinlink.ui.CallConnectActivity;
import com.dinstar.dinlink.ui.CallIncomingActivity;
import com.dinstar.dinlink.ui.CallOutgoingActivity;
import com.dinstar.dinlink.ui.LinphoneMainActivity;
import com.dinstar.linphone.BaseActivity;

import org.linphone.core.Call;


public class MyBroadcastReceiver extends BroadcastReceiver {
    private static final String TAG = "MyBroadcastReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {

        Log.i(TAG, "onReceive:收到了锁屏消息 ");
        String action = intent.getAction();

        if (action.equals("com.okcis.siplibrary.CallState")){
            int callState = intent.getIntExtra("CallState", -1);
            if (callState != -1){
                Activity currentActivity = BaseActivity.getCurrentActivity();
                if (callState == Call.State.IncomingReceived.toInt()){
                    Intent startIntent = new Intent(context, CallIncomingActivity.class);
                    startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(startIntent);
                } else if (callState == Call.State.OutgoingInit.toInt()){
                    Intent startIntent = new Intent(context, CallOutgoingActivity.class);
                    startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(startIntent);
                } else if (callState == Call.State.Connected.toInt()){
                    if (currentActivity instanceof CallOutgoingActivity){
                        currentActivity.finish();
                    } else if (currentActivity instanceof CallIncomingActivity){
                        currentActivity.finish();
                    }
                    Intent startIntent = new Intent(context, CallConnectActivity.class);
                    startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(startIntent);
                } else if (callState == Call.State.End.toInt()){
                    if (currentActivity instanceof LinphoneMainActivity){

                    } else if (currentActivity instanceof CallOutgoingActivity){
                        currentActivity.finish();
                    } else if (currentActivity instanceof CallIncomingActivity){
                        currentActivity.finish();
                    } else if (currentActivity instanceof CallConnectActivity){
                        currentActivity.finish();
                    }

                } else if (callState == Call.State.Released.toInt()){
                    if (currentActivity instanceof LinphoneMainActivity){

                    } else if (currentActivity instanceof CallOutgoingActivity){
                        currentActivity.finish();
                    } else if (currentActivity instanceof CallIncomingActivity){
                        currentActivity.finish();
                    } else if (currentActivity instanceof CallConnectActivity){
                        currentActivity.finish();
                    }
                } else if (callState == Call.State.Error.toInt()){
                    if (currentActivity instanceof LinphoneMainActivity){

                    } else if (currentActivity instanceof CallOutgoingActivity){
                        currentActivity.finish();
                    } else if (currentActivity instanceof CallIncomingActivity){
                        currentActivity.finish();
                    } else if (currentActivity instanceof CallConnectActivity){
                        currentActivity.finish();
                    }
                }
            }
        }
//        else  if (intent.getAction().equals("com.zx.lockscreenmsgdemo.LockScreenMsgReceiver")) {
//            //管理锁屏的一个服务
//            KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
//            String text = km.inKeyguardRestrictedInputMode() ? "锁屏了" : "屏幕亮着的";
//            Log.i(TAG, "text: " + text);
//            if (km.inKeyguardRestrictedInputMode()) {
//                Log.i(TAG, "onReceive:锁屏了 ");
//                //判断是否锁屏
//                Intent alarmIntent = new Intent(context, MessageActivity.class);
//                //在广播中启动Activity的context可能不是Activity对象，所以需要添加NEW_TASK的标志，否则启动时可能会报错。
//                alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                context.startActivity(alarmIntent); //启动显示锁屏消息的activity
//            }
//        }

    }
}
